package cn.org.zx.common.commutils;

import java.util.ArrayList;
import java.util.List;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class  CommMethod 

{
	/**
	 * 字符串的字符集类型转换

	 * 
	 * @param src
	 *            需要转换的字符串

	 * @param fromCharSet
	 *            字符串当前的字符集类型，如"iso-8859-1","GBK"等

	 * @param toCharSet
	 *            目标字符集类型，如"iso-8859-1","GBK"等

	 * @return 转换后的字符串,失败返回null
	 */
	public static String charSetConvert(String src, String fromCharSet, String toCharSet) {
		if (src == null) {
			return src;
		}
		try {
			return new String(src.getBytes(fromCharSet), toCharSet);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
    	
	/**
	 * 将iso8859的字符集转换成UTF-8字符集

	 * 
	 * @param src
	 *            iso8859字符串

	 * @return 转化后的字符串,失败返回null
	 */
	public static String isoToUTF8(String src) {
		String arch = System.getProperty("os.arch");
		if ("x86".equalsIgnoreCase(arch)) {
			return src;
		}
		return charSetConvert(src, "iso-8859-1", "UTF-8");
	}

	/**
	 * 将UTF-8的字符集转换成iso8859字符集

	 * 
	 * @param src
	 *            UTF-8字符串

	 * @return 转化后的字符串,失败返回null
	 */
	public static String utf8ToISO(String src) {
		String arch = System.getProperty("os.arch");
		if ("x86".equalsIgnoreCase(arch)) {
			return src;
		}
		return charSetConvert(src, "UTF-8", "iso-8859-1");
	}

	/**
	 * 
	 * @将iso8859的字符集转换成UTF-8字符集

	 * @param src
	 *            iso8859字符串,force 强制转换标志
	 * @return 转化后的字符串,失败返回null
	 */
	public static String isoToUTF8(String src, boolean force) {
		if (force) {
			return charSetConvert(src, "iso-8859-1", "UTF-8");
		} else {
			return isoToUTF8(src);
		}
	}

	/**
	 * 将iso8859的字符集转换成GBK字符集

	 * 
	 * @param src
	 *            iso8859字符串

	 * @return 转化后的字符串,失败返回null
	 */
	public static String isoToGBK(String src) {
		return charSetConvert(src, "iso-8859-1", "GBK");
	}

	/**
	 * 将GBK的字符集转换成iso8859字符集

	 * 
	 * @param src
	 *            GBK字符串

	 * @return 转化后的字符串,失败返回null
	 */
	public static String gbkToISO(String src) {
		return charSetConvert(src, "GBK", "iso-8859-1");
	}
	
	
	public static String DecoderToUTF8(String s)
    {
		String s1 = "";
		String s2 = "";
        try
        {	
        	if(!s.equals("")){
				s1 = java.net.URLDecoder.decode(s,"UTF-8"); 
				s2 = strDealNull(s1);
			}
            return s2;
        }
        catch(Exception exception)
        {
            return s;
        }
    }
	
	/**
	 * 重复一个字符串 n 次，比如 abcabcabc
	 * 
	 * @param str
	 *            需要重复的字符串

	 * @param repeat
	 *            重复的次数

	 * @return 重复后生成的字符串

	 */
	public static String repeat(String str, int repeat) {
		StringBuffer buffer = new StringBuffer(repeat * str.length());
		for (int i = 0; i < repeat; i++) {
			buffer.append(str);
		}
		return buffer.toString();
	}

	/**
	 * 获得一个字符串的最左边的n个字符，如果长度len的长度大于字符串的总长度，返回字符串本身

	 * 
	 * @param str
	 *            原始的字符串
	 * @param len
	 *            左边的长度

	 * @return 最左边的字符

	 */
	public static String left(String str, int len) {
		if (len < 0) {
			throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
		}
		if ((str == null) || (str.length() <= len)) {
			return str;
		} else {
			return str.substring(0, len);
		}
	}

	/**
	 * 获得一个字符串的最右边的n个字符，如果长度len的长度大于字符串的总长度，返回字符串本身

	 * 
	 * @param str
	 *            原始的字符串
	 * @param len
	 *            右边的长度

	 * @return 最右边的字符

	 */
	public static String right(String str, int len) {
		if (len < 0) {
			throw new IllegalArgumentException("Requested String length " + len + " is less than zero");
		}
		if ((str == null) || (str.length() <= len)) {
			return str;
		} else {
			return str.substring(str.length() - len);
		}
	}
	
	
	/**
	 * 格式化字符串,将字符串trim()后返回. 如果字符串为null,则返回长度为零的字符串("")
	 * 
	 * @param value
	 *            被格式化字符串

	 * @return 格式化后的字符串
	 */
	public static String stringTrim(String value) {
		if (value == null) {
			return "";
		}
		return value.trim();
	}
   
	/**
	 * 将"null"和null转换为""
	 * 
	 * @param str
	 *            String
	 * @return String
	 */
    public static String strDealNull(String s)
    {
        String s1 = "";
        if(s == null || s.trim().equals("") || s.trim().equalsIgnoreCase("null"))
            s1 = "";
        else
            s1 = s;
        
        s1 = s1.replace("'", "‘");
        
        return s1;
    }
	
    public static String strReplace(String s, String s1, String s2)
    {
        String as[] = strSplit(s, s1);
        String s3 = null;
        if(as.length != 0)
        {
            s3 = as[0];
            for(int i = 0; i < as.length - 1; i++)
                s3 = strDealNull(s3) + s2 + as[i + 1];

        }
        return strDealNull(s3);
    }
    
    public static String[] strSplit(String s, String s1)
    {
        int i = 0;
        int j = 0;
        boolean flag = false;
        int i1 = s1.length();
        if(s.compareTo("") != 0)
        {
            if(s.indexOf(s1) != -1)
            {
                j = s.indexOf(s1);
                int j1 = 1;
                do
                {
                    if(s.indexOf(s1, j + i1) != -1)
                    {
                        j = s.indexOf(s1, j + i1);
                        i = j1;
                    } else
                    {
                        i += 2;
                        break;
                    }
                    j1++;
                } while(true);
            } else
            {
                i = 1;
            }
        } else
        {
            i = 0;
        }
        j = 0;
        flag = false;
        String as[] = new String[i];
        if(s.compareTo("") != 0)
        {
            if(s.indexOf(s1) != -1)
            {
                int k = s.indexOf(s1);
                as[0] = s.substring(0, k);
                int k1 = 1;
                do
                {
                    if(s.indexOf(s1, k + i1) != -1)
                    {
                        int l = s.indexOf(s1, k + i1);
                        as[k1] = s.substring(k + i1, l);
                        k = s.indexOf(s1, k + i1);
                    } else
                    {
                        as[k1] = s.substring(k + i1, s.length());
                        break;
                    }
                    k1++;
                } while(true);
            } else
            {
                as[0] = s.substring(0, s.length());
                return as;
            }
        } else
        {
            return as;
        }
        return as;
    }
	
    public static boolean matcher(String patternString,String beMatcheredString)
	{
		boolean bReturn = false;
		Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(beMatcheredString);
		if (matcher.find()) 
			bReturn  = true;
		return bReturn;
	}
    
    public static String listToStr(List l){
		String strReturn = "";
		for(int i=0;i<l.size();i++)
			strReturn += l.get(i)+","; 
		if(strReturn.length()!=0)
		strReturn = strReturn.substring(0,strReturn.length()-1);
		return strReturn;
	}
    

    /**
	 * 产生随机临时表的名称
	 * @return
	 */
    public static String createTN()
	{
		return "Temp" + RandomString.randomNumeric(8);
	}
   
	public static void main(String args[])
	{
		try
        {	
			System.out.println(getProjectDir());
        }
        catch(Exception ex){ 
			ex.printStackTrace();
		}
    }
	/**
	 * 转换XML保留字符 
	 * @param childNode
	 * @return
	 */
	public static String transXmlFields(String childNode)
	{
	 String strTransedNodes = "";
	 if(childNode!=null)
	 {
		 strTransedNodes = childNode;
		 strTransedNodes = strTransedNodes.replaceAll("&", "&amp;");
	 }
	 return strTransedNodes;
	} 
	
	
	public static String unTransXmlFields(String childNode)
	{
	 String strTransedNodes = "";
	 if(childNode!=null)
	 {
		 strTransedNodes = childNode;
		 strTransedNodes = strTransedNodes.replaceAll("&amp;","&");
	 }
	 return strTransedNodes;
	} 
	
	public static String getProjectDir()
	{
		String strDir = "";
		strDir = java.net.URLDecoder.decode(Thread.currentThread().getContextClassLoader().getResource("")+"../../").substring(6);
		return strDir;
	}
	
	//获取网页中所有图片的路径
	public static List<String> getImageSrc(String htmlCode) {
	    List<String> imageSrcList = new ArrayList<String>();
	    Pattern p = Pattern.compile("<img\\b[^>]*\\bsrc\\b\\s*=\\s*('|\")?([^'\"\n\r\f>]+(\\.jpg|\\.bmp|\\.eps|\\.gif|\\.mif|\\.miff|\\.png|\\.tif|\\.tiff|\\.svg|\\.wmf|\\.jpe|\\.jpeg|\\.dib|\\.ico|\\.tga|\\.cut|\\.pic)\\b)[^>]*>", Pattern.CASE_INSENSITIVE);
	    Matcher m = p.matcher(htmlCode);
	    String quote = null;
	    String src = null;
	    while (m.find()) {
	        quote = m.group(1); 
	        src = (quote == null || quote.trim().length() == 0) ? m.group(2).split("\\s+")[0] : m.group(2);
	        imageSrcList.add(src);	 
	    }
	    return imageSrcList;
	}	
}
